from es_clienter import ssc
import json



class query_generator:
    def __init__(self, search_key, search_value):
        self.search_key = search_key
        self.search_value = search_value
    def create(self):
        query = {
            "match":{
                str(self.search_key):str(self.search_value)
            }
        }
        return query


class Searcher:
    def __init__(self):
        self.client = ssc

    def simple_search(self, key_tuples, table_id=None, sheet_id=None, file_name=None):
        # 仅做搜索
        len_of_key_tuples = len(key_tuples)
        query_map = {}
        if len_of_key_tuples == 0:
            print("找不到搜索条件")
            return
        elif len_of_key_tuples == 1:
            print("单条件搜索")
            key_tuple = key_tuples[0]
            print(f"搜索条件: {key_tuple}")
            len_of_key_tuple = len(key_tuple)

            must_list = []
            for query_point in key_tuple:
                must_list.append(query_generator("cells.relationships", query_point).create())
            query_map = {
                "bool":{
                    "must":must_list
                }
            }
        else:
            print("多条件搜索")
            shoud_list = []
            for key_tuple in key_tuples:
                print(f"搜索条件: {key_tuple}")
                must_list = []
                for query_point in key_tuple:
                    must_list.append(query_generator("cells.relationships", query_point).create())
                tiny_query_map = {
                    "bool":{
                        "must": must_list
                    }
                }
                shoud_list.append(tiny_query_map)
            query_map = {
                "bool":{
                    "should": shoud_list
                }
            }
        query_condition = {
            "nested":{
                "path": "cells",
                "query": query_map
            },
            "inner_hits":{

            }
        }
        print(query_condition)

        if sheet_id is not None:
            query_condition["sheet_id"]= sheet_id
        if file_name is not None:
            query_condition["excel_name"] = file_name
        
        search_result = self.client.search(query_condition)
            
    def advance_search(self, key_tuple, method):
        # 搜索 + 聚合函数
        pass


searcher = Searcher()

if __name__ == "__main__":
    ad = {
        "{0}":"aaa"
    }
    str(ad).format("test")
    print(ad)